package service;

import common.Pagination;
import common.StringUtils;
import dao.CroFeeDao;
import entity.CroFee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vo.CroFeeVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/12 0012.
 */
@Service
public class CroFeeService {
    @Autowired
    private CroFeeDao croFeeDao;

    /*
    * 查找所有费用信息
    * */
    @Transactional(readOnly = true)
    public List<CroFee> FindFee(){
        String hql="from CroFee where del_flag = 0";
        List<CroFee> croFees=croFeeDao.findList(hql);
        return croFees;
    }

    /*
    * 添加费用信息
    * */
    @Transactional(readOnly = false)
    public boolean addFee(CroFee croFee){
        try {
            croFeeDao.save(croFee);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*查找单个用户*/
    @Transactional(readOnly = true)
    public CroFee FindFeeById(String id){
        CroFee croFees=croFeeDao.getById(id);
        return croFees;
    }
    /*
   * 分页查找出全部费用信息
   * */
    @Transactional(readOnly = true)
    public Pagination<CroFee> findAllUser(int index, int size){
        String hql="from CroFee where del_flag='0'";
        Pagination<CroFee> croUserList=croFeeDao.findPagination(hql,index,size);
        return croUserList;
    }
    /*
    * 删除费用信息
    * */
    @Transactional(readOnly = false)
    public boolean delFeeById(String id){
        try{
            CroFee croFee = croFeeDao.getById(id);
            croFee.setDel_flag("1");
            croFeeDao.update(croFee);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /*
    * 更新费用信息
    * */
    @Transactional(readOnly = false)
    public boolean updateFee(CroFee croFee){
        try{
            croFeeDao.update(croFee);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /*
    * 根据属性来查找费用信息
    * */
    @Transactional(readOnly = true)
    public Pagination<CroFee> FindUserByProperty(CroFeeVo croFeeVo, int index, int size){
        String hql="from CroFee where del_flag='0' ";
        StringBuffer sb=new StringBuffer();
        sb.append(hql);
        if(StringUtils.isNotBlank(croFeeVo.getName())){
            sb.append(" and name like '%"+croFeeVo.getName()+"%'");
        }
        if(StringUtils.isNotBlank(croFeeVo.getType())){
            sb.append(" and type='"+croFeeVo.getType()+"'");
        }
        if (StringUtils.isNotBlank(croFeeVo.getStarcost()) && StringUtils.isNotBlank(croFeeVo.getEndcost())){
            sb.append(" and cost between '"+croFeeVo.getStarcost()+"' and '"+croFeeVo.getEndcost()+"'");
        }else if (StringUtils.isNotBlank(croFeeVo.getStarcost()) && !StringUtils.isNotBlank(croFeeVo.getEndcost())){
            sb.append(" and cost >='"+croFeeVo.getStarcost()+"'");
        }else if(!StringUtils.isNotBlank(croFeeVo.getStarcost()) && StringUtils.isNotBlank(croFeeVo.getEndcost())){
            sb.append(" and cost <='"+croFeeVo.getEndcost()+"'");
        }
        Pagination<CroFee> croFee=croFeeDao.findPagination(sb.toString(),index,size);
        return croFee;
    }

    /*
    * 查询快递费用
    * */
    @Transactional(readOnly = true)
    public CroFee Findkuaidi(){
        Map<String,Object> map=new HashMap<>();
        map.put("del_flag","0");
        map.put("type","0");
        List<CroFee> croFees=croFeeDao.findByProperty(map);
        return croFees.get(0);
    }
}
